package com.example.actionsignin.dialog;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TimePicker;

import com.example.actionsignin.R;
import com.example.actionsignin.mvp.presenter.IReleaseListener;

import java.util.Calendar;

/**
 * Created by 旭 on 2017/7/29/029.
 */

public class EndTimeDialogFragment extends DialogFragment implements
        TimePicker.OnTimeChangedListener,
        View.OnClickListener{

    private View view;

    private TimePicker timePicker;

    private int mHour,mMinute;

    private String mTime;
    private String mDate;
    private String date;

    private IReleaseListener releaseListener;

    @Override
    public void onStart() {
        super.onStart();
        DisplayMetrics displayMetrics = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        getDialog().getWindow().setLayout((int) (displayMetrics.widthPixels * 0.9f),getDialog().getWindow().getAttributes().height);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        getDialog().requestWindowFeature(STYLE_NO_TITLE);
        view = inflater.inflate(R.layout.fragment_dialog_end_time,container,false);

        Bundle bundle = getArguments();
        mDate = bundle.getString("date");

        getNowTime();
        initDate();
        initView();

        releaseListener = (IReleaseListener) getActivity();

        return view;
    }

    private void initDate() {
        date = mDate +  mTime;
    }

    private void initView() {
        view.findViewById(R.id.end_time_ok).setOnClickListener(this);
        view.findViewById(R.id.end_time_cancel).setOnClickListener(this);
        timePicker = (TimePicker) view.findViewById(R.id.end_time_timePicker);
        timePicker.setOnTimeChangedListener(this);
        timePicker.setIs24HourView(true);

        timePicker.setCurrentHour(mHour);
        timePicker.setCurrentMinute(mMinute);
    }

    private void getNowTime()
    {
        Calendar calendar = Calendar.getInstance();
        mHour = calendar.get(Calendar.HOUR_OF_DAY);
        mMinute = calendar.get(Calendar.MINUTE);

        mTime = mHour + ":" + mMinute;
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id)
        {
            case R.id.end_time_ok:
                releaseListener.sendData(2,date);
                dismiss();
                break;
            case R.id.end_time_cancel:
                dismiss();
                break;
        }
    }

    @Override
    public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
        mTime = hourOfDay + ":" + minute;
        date = mDate + mTime;
    }
}
